package com.lancoo.answer.ui.child.lookReview;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.widget.combinationView.AnswerAnalysisVPView;
import com.lancoo.answer.widget.combinationView.MatchQueseBodyView;
import com.lancoo.answer.widget.span.view.MatchFillCallBack;

import java.util.List;


/**
 * Created by pengmi on 2021/12/1 20:53
 * Description:段落匹配小题查看评阅fragment
 */
public class ChildMatchLookReviewFragment extends Fragment implements MatchFillCallBack {

    private Child child;
    private static final String TAG = "QueseMatchFragment";
    private MatchQueseBodyView queseBodyView;
    private AnswerAnalysisVPView answerAnalysisView;
    private int typeIndex;
    private int queseIndex;
    private int childIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_child_match_look_review, container, false);
    }

    public static Fragment loadChildFragment(int kindIndex, int queseIndex, int smallQueseIndex, int defaultLoadIndex){
        ChildMatchLookReviewFragment queseFragment=new ChildMatchLookReviewFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,smallQueseIndex);
        mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX,defaultLoadIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){
        if(getArguments()==null){
            return;
        }
        typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        queseIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        childIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        int loadChildIndex=getArguments().getInt(AnswerConstant.KEY_LOADCHILDINDEX);
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        child = kindBeanListk.get(typeIndex).getQuesList().get(queseIndex).getChildList().get(childIndex);
        queseBodyView = view.findViewById(R.id.quese_body_view);
        if(loadChildIndex>=0){
            queseBodyView.setShowIndex(loadChildIndex);
        }
        queseBodyView.setCallBack(this);
        queseBodyView.initData(child);
        answerAnalysisView = view.findViewById(R.id.answer_analysis_view);
        answerAnalysisView.setItemList(child.getItemList(),loadChildIndex);
        answerAnalysisView.setOnAnswerAnalysisPageChangeCallback(new AnswerAnalysisVPView.OnAnswerAnalysisPageChangeCallback(){
            @Override
            public void onPageSelect(int position) {
                super.onPageSelect(position);
                queseBodyView.setShowIndex(position);
                queseBodyView.initData(child);
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        if(queseBodyView!=null){
            queseBodyView.pause();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(queseBodyView!=null){
            queseBodyView.release();
        }
    }

    @Override
    public void onClick(final int position) {
        Log.e(TAG,"onClick"+position);
        queseBodyView.initData(child);
        answerAnalysisView.setViewPager2CurrentIndex(position);
    }

    public int getCurrentItemIndex(){
        if(queseBodyView!=null){
            return queseBodyView.getShowIndex();
        }
        return 0;
    }

    public void adjustFontSize(){
        if(queseBodyView==null){
            return;
        }
        queseBodyView.adjustFontSize();
    }
}
